package com.score.intercepter;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MethodLogPrintUtils {

    private static Logger log = LoggerFactory.getLogger(MethodLogPrintUtils.class);

    public static void methodProcessStart(ProceedingJoinPoint joinPoint) {
        StringBuilder argsBuff = new StringBuilder("-->>参数列表:");
        try {
            Object[] argVals = joinPoint.getArgs();
            if (argVals != null && argVals.length > 0) {
                int i = 0;
                for (Object argVal : argVals) {
                    if (i > 0) {
                        argsBuff.append(",");
                    }
                    argsBuff.append(argVal);
                    i++;
                }
            }
        } catch (Exception e) {
            log.info("获取方法参数失败:", e);
        }
        argsBuff.append("-->>").append(argsBuff.toString()).append("-->>共").append(joinPoint.getArgs().length)
                .append("个参数。").append("-->>访问了").append(joinPoint.getTarget()).append("类的")
                .append(joinPoint.getSignature().getName()).append("方法。");
        log.info(argsBuff.toString());
    }

    public static void methodProcessEnd(ProceedingJoinPoint joinPoint, long userTimes) {
        String argsBuff = "-->>访问" + joinPoint.getTarget() + "类的" + joinPoint.getSignature().getName() + "方法结束。" +
                "-->>耗时：" + userTimes + "ms";
        log.info(argsBuff);
    }
}
